<script lang="ts" setup>
import { computed } from 'vue';

import { useAntdDesignTokens } from '@vben/hooks';
import { preferences, usePreferences } from '@vben/preferences';

import { App, ConfigProvider, theme } from 'ant-design-vue';

import { antdLocale } from '#/locales';
import { useDictStore } from '#/store/dict';
import { useAuthStore } from '#/store';
import { useAccessStore } from '@vben/stores';

//  添加图标库
import { addCollection } from '@vben/icons';
import AntDesignIcons from '@iconify/json/json/ant-design.json';
addCollection(AntDesignIcons);

defineOptions({ name: 'App' });

const { isDark } = usePreferences();
const { tokens } = useAntdDesignTokens();

const tokenTheme = computed(() => {
  const algorithm = isDark.value
    ? [theme.darkAlgorithm]
    : [theme.defaultAlgorithm];

  // antd 紧凑模式算法
  if (preferences.app.compact) {
    algorithm.push(theme.compactAlgorithm);
  }

  return {
    algorithm,
    token: tokens,
  };
});

// InitAPP  启动时执行
// 启动时执行，用于初始化数据，如：获取字典信息、菜单信息等
const InitAPP = () => {
  console.log('InitAPP', useAccessStore()?.accessToken);
  useDictStore().initDictQuery();
  if (useAccessStore()?.accessToken) {
    console.log('InitAPP', '这里判断是登录状态');
    useAuthStore().fetchUserInfo();
    // useDictStore().initDictQuery();
    // useDictStore().initDictQuery();
  }
};
InitAPP();
</script>

<template>
  <ConfigProvider :locale="antdLocale" :theme="tokenTheme">
    <App>
      <RouterView />
    </App>
  </ConfigProvider>
</template>
